﻿using System;
using System.Collections.Generic;
using DesignByContract;
using MadMediaManager.Core.ExtensionMethods;

namespace MadMediaManager.DataAccess.Database
{
    public enum DatabaseType
    {
        SqlCe,
        SqlCe40,
        Sql2000,
        Sql2005,
        Sql2008
    }

    public class NHiberanteConfigurationPropertiesFactory
    {
        private readonly IDictionary<string, string> _properties = new Dictionary<string, string>();

        public NHiberanteConfigurationPropertiesFactory(DatabaseType databaseType, string connectionString, bool showSql)
        {
            Check.Require(!connectionString.IsNullOrEmpty(), "connectionString cannot be null.");
            string dialect;
            string driver;
            switch (databaseType)
            {
                case DatabaseType.SqlCe:
                    dialect = typeof(NHibernate.Dialect.MsSqlCeDialect).AssemblyQualifiedName;
                    driver = typeof(NHibernate.Driver.SqlServerCeDriver).AssemblyQualifiedName;
                    break;
                case DatabaseType.SqlCe40:
                    dialect = typeof(NHibernate.Dialect.MsSqlCe40Dialect).AssemblyQualifiedName;
                    driver = typeof(NHibernate.Driver.SqlServerCeDriver).AssemblyQualifiedName;
                    break;
                case DatabaseType.Sql2000:
                    dialect = typeof(NHibernate.Dialect.MsSql2000Dialect).AssemblyQualifiedName;
                    driver = typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName;
                    break;
                case DatabaseType.Sql2005:
                    dialect = typeof(NHibernate.Dialect.MsSql2005Dialect).AssemblyQualifiedName;
                    driver = typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName;
                    break;
                case DatabaseType.Sql2008:
                    dialect = typeof(NHibernate.Dialect.MsSql2008Dialect).AssemblyQualifiedName;
                    driver = typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName;
                    break;
                default:
                    throw new NotSupportedException(databaseType + " is not supported.");
            }

            _properties["dialect"] = dialect;
            _properties["connection.driver_class"] = driver;
            _properties["connection.connection_string"] = connectionString;

            _properties["show_sql"] = showSql ? bool.TrueString : bool.FalseString;

            _properties["connection.provider"] =
                typeof(NHibernate.Connection.DriverConnectionProvider).AssemblyQualifiedName;

            _properties["connection.release_mode"] = "auto";
            _properties["adonet.batch_size"] = "500";
            _properties["proxyfactory.factory_class"] =
                typeof(NHibernate.ByteCode.Castle.ProxyFactoryFactory).AssemblyQualifiedName;
        }

        public IDictionary<string, string> Properties
        {
            get { return _properties; }
        }
    }
}
